﻿namespace Pp.UI.Models
{
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;
    using System.Web.Security;
    using Core;
    using Dto;
    using Dto.Validators;
    using Repository;

    #region Models

    public class ChangePasswordModel
    {
        [EmptyValidator, MaxLengthValidator(64, 5)]
        public string OldPassword { get; set; }

        [EmptyValidator, MaxLengthValidator(64, 5)]
        public string NewPassword { get; set; }

        [EmptyValidator, MaxLengthValidator(64, 5), Compare("NewPassword", ErrorMessage = "Şifreler uyumlu değil")]
        public string ConfirmPassword { get; set; }
    }

    public class LogOnModel
    {
        [EmptyValidator, MaxLengthValidator(64, 5)]
        public string UserName { get; set; }

        [EmptyValidator, MaxLengthValidator(64, 5)]
        public string Password { get; set; }

        public bool RememberMe { get; set; }
    }


    public class RegisterModel
    {
        public string City { get; set; }
        public bool? IsMan { get; set; }
        public DateTime? BirthDay { get; set; }
        public string WorkInfo { get; set; }

        public string EducationalInfo { get; set; }
        [EmptyValidator, MaxLengthValidator(64, 5)]
        public string UserName { get; set; }
        [EmptyValidator, MaxLengthValidator(120, 15), EmailValidator]
        public string Email { get; set; }
        [EmptyValidator, MaxLengthValidator(64, 5)]
        public string Password { get; set; }
        [MaxLengthValidator(64, 5), Compare("Password", ErrorMessage = "Şifreler uyumlu değil")]
        public string ConfirmPassword { get; set; }
    }
    #endregion

    #region Services

    public interface IMembershipService
    {
        int MinPasswordLength { get; }
        bool ValidateUser(string userName, string password);
        Result CreateUser(string userName, string password, string email);
        Result ChangePassword(string userName, string oldPassword, string newPassword);
    }

    public class AccountMembershipService : IMembershipService
    {
        private readonly UserRepository _userRepository;
        public AccountMembershipService(UserRepository userRepository)
        {
            _userRepository = userRepository;
        }


        public int MinPasswordLength
        {
            get
            {
                return 5;
            }
        }

        public bool ValidateUser(string userName, string password)
        {
            if (String.IsNullOrEmpty(userName) || String.IsNullOrEmpty(password))
                throw new BussinessException("Boş veya null olan alan bırakmayınız");
            if (password.Length < MinPasswordLength)
                throw new BussinessException("Şifre 5 karakterden küçük olamaz.");
            return _userRepository.ValidateUser(userName, password);
        }

        public Result CreateUser(string userName, string password, string email)
        {
            if (String.IsNullOrEmpty(password) || String.IsNullOrEmpty(email) || String.IsNullOrEmpty(userName))
                throw new BussinessException("Boş veya null olan alan bırakmayınız");
            if (password.Length < MinPasswordLength)
                throw new BussinessException("Şifre 5 karakterden küçük olamaz.");
            Result status;
            _userRepository.CreateUser(userName, password, email, out status);
            return status;
        }

        public Result ChangePassword(string userName, string oldPassword, string newPassword)
        {
            if (String.IsNullOrEmpty(userName) || (String.IsNullOrEmpty(oldPassword)) || (String.IsNullOrEmpty(newPassword)))
                return Result.AsError("Boş alan bırakmayınız");
            if (newPassword.Length < MinPasswordLength)
                throw new BussinessException("Şifre 5 karakterden küçük olamaz.");
            try
            {
                return _userRepository.ChangePassword(userName, oldPassword, newPassword);
            }
            catch (BussinessException ex)
            {
                return Result.AsError(ex.MessageText);
            }
            catch (MembershipPasswordException ex)
            {
                return Result.AsError(ex.Message);
            }
        }

    }

    public interface IFormsAuthenticationService
    {
        void SignIn(string userName, bool createPersistentCookie);
        void SignOut();
    }

    public class FormsAuthenticationService : IFormsAuthenticationService
    {
        public void SignIn(string userName, bool createPersistentCookie)
        {
            if (String.IsNullOrEmpty(userName))
                throw new BussinessException("Boş veya null olan alan bırakmayınız");
            FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
        }

        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }
    }
    #endregion

}

